﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp_Event
{
    /// <summary>
    /// MouseEventWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MouseEventWindow : Window
    {
        private Point _mouseStartPoint = new Point(0, 0);
        private double _left = 0;
        private double _top = 0;
        private bool _isMove = false;

        private bool _isCanvasMove = false;

        public MouseEventWindow()
        {
            InitializeComponent();
        }

        #region Border移动

        private void Border_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_isMove)
                return;
            Border _operBorder = (Border)sender;
            Point _nowPosition = e.GetPosition(this);
            double _offset_x = _nowPosition.X - _mouseStartPoint.X;
            double _offset_y = _nowPosition.Y - _mouseStartPoint.Y;
            Canvas.SetLeft(_operBorder, _offset_x + _left);
            Canvas.SetTop(_operBorder, _offset_y + _top);
            e.Handled= true;
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Border _operBorder = (Border)sender;
            _mouseStartPoint = e.GetPosition(this);
            _left = Canvas.GetLeft(_operBorder);
            _top = Canvas.GetTop(_operBorder);
            _isMove = true;
            //将对象绑定再光标上
            Mouse.Capture(_operBorder);
        }

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _mouseStartPoint = new Point(0, 0);
            _left = 0;
            _top = 0;
            _isMove = false;
            Mouse.Capture(null);
            e.Handled = false;
        }

        #endregion


        #region 框选

        Rectangle rectangle = new Rectangle()
        {
            Stroke = Brushes.Red,
            StrokeThickness = 1,
            StrokeDashArray = new DoubleCollection { 2, 2 }
        };

        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _mouseStartPoint = e.GetPosition(this.canvas);
            this.canvas.Children.Add(rectangle);
            Canvas.SetLeft(rectangle, _mouseStartPoint.X);
            Canvas.SetTop(rectangle, _mouseStartPoint.Y);
            rectangle.Width = 0;
            rectangle.Height = 0;
            _isCanvasMove = true;
        }

        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.canvas.Children.Remove(rectangle);
            _isCanvasMove = false;
        }

        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_isCanvasMove)
                return;
            Point _nowPoint = e.GetPosition(this.canvas);
            rectangle.Width = _nowPoint.X - _mouseStartPoint.X;
            rectangle.Height = _nowPoint.Y - _mouseStartPoint.Y;
        }

        #endregion


    }
}
